using System;
using System.Collections.Generic;
using System.Text;
using GarageGames.Torque.Sim;
using Microsoft.Xna.Framework.Input;
using GarageGames.Torque.Platform;
using Xyne.Regions;
using GarageGames.Torque.XNA;
using System.Collections;
using GarageGames.Torque.Core;
using GarageGames.Torque.T2D;
using Microsoft.Xna.Framework;
using Xyne.GUI;
using Xyne.Events;

namespace Xyne.Utilities
{
    class PlayerController
    {
        #region Private Vars
        private HandRegion handRegion;
        
        private Queue<int> cardButtonQueue;

        private int[] cardButtonTracker;

        #endregion

        #region Constructor
        public PlayerController(HandRegion handRegion)
        {
            this.handRegion = handRegion;
            cardButtonQueue = new Queue<int>();
            cardButtonTracker = new int[4];
            for (int i = 0; i < 4; i++)
            {
                cardButtonTracker[i] = -1;
            }
        }

        public void Init()
        {
            InputMap inputMap = PlayerManager.Instance.GetPlayer(0).InputMap;
            int keyboardId = InputManager.Instance.FindDevice("keyboard");
            if (keyboardId >= 0)
            {
                inputMap.BindAction(keyboardId, (int)Keys.A, OnA);
                inputMap.BindAction(keyboardId, (int)Keys.B, OnB);
                inputMap.BindAction(keyboardId, (int)Keys.X, OnX);
                inputMap.BindAction(keyboardId, (int)Keys.Y, OnY);
                inputMap.BindAction(keyboardId, (int)Keys.Space, OnPause);
                inputMap.BindAction(keyboardId, (int)Keys.Escape, OnPause);
            }
            int gamepadId = InputManager.Instance.FindDevice("gamepad0");
            if (gamepadId >= 0)
            {
                inputMap.BindAction(gamepadId, (int)XGamePadDevice.GamePadObjects.A, OnA);
                inputMap.BindAction(gamepadId, (int)XGamePadDevice.GamePadObjects.B, OnB);
                inputMap.BindAction(gamepadId, (int)XGamePadDevice.GamePadObjects.X, OnX);
                inputMap.BindAction(gamepadId, (int)XGamePadDevice.GamePadObjects.Y, OnY);
                inputMap.BindAction(gamepadId, (int)XGamePadDevice.GamePadObjects.Start, OnPause);
            }
        }
        #endregion

        #region Events
        private void OnA(float val)
        {
            if (val > 0)
            {
                ButtonPressed(0);
            }
            else
            {
                ButtonUnpressed(0);
            }
        }

        private void OnB(float val)
        {
            if (val > 0)
            {
                ButtonPressed(1);
            }
            else
            {
                ButtonUnpressed(1);
            }
        }

        private void OnX(float val)
        {
            if (val > 0)
            {
                ButtonPressed(2);
            }
            else
            {
                ButtonUnpressed(2);
            }
        }

        private void OnY(float val)
        {
            if (val > 0)
            {
                ButtonPressed(3);
            }
            else
            {
                ButtonUnpressed(3);
            }
        }

        private void OnPause(float val)
        {
            if (val > 0)
            {
                if (Game.Network != null)
                {
                    Game.Network.HandleEvent(new GamePauseEvent(this, true, Game.Network.Gamer != null ? Game.Network.Gamer.Gamertag : String.Empty));
                }
            }
        }
        #endregion

        #region Methods
        private void ButtonPressed(int button)
        {
            bool cardSelected = false;
            for (int i = 0; i < 4; i++)
            {
                if (cardButtonTracker[i] != -1)
                {
                    cardSelected = true;
                }
            }
            if (!cardSelected)
            {
                if (handRegion.CardPressed(button))
                {
                    cardButtonTracker[button] = 0;
                }
            }
            else
            {
                int nextInLine = 0;
                for (int i = 0; i < 4; i++)
                {
                    if (nextInLine <= cardButtonTracker[i])
                    {
                        nextInLine = cardButtonTracker[i] + 1;
                    }
                }
                cardButtonTracker[button] = nextInLine;
            }
        }

        private void ButtonUnpressed(int button)
        {
        /*    if (currentCardPress != -1 && currentCardPress == button)
            {
                handRegion.CardDepressed(button);
                if (cardButtonQueue.Count > 0)
                {
                    currentCardPress = cardButtonQueue.Dequeue();
                    handRegion.CardPressed(currentCardPress);
                }
                else
                {
                    currentCardPress = -1;
                }
            }*/


            if (cardButtonTracker[button] > -1)
            {
                int nextButton = -1;
                int currentButton = button;
                if (cardButtonTracker[button] == 0)
                {
                    handRegion.CardDepressed(button);
                }
                do
                {
                    nextButton = -1;
                    for (int i = 0; i < 4; i++)
                    {
                        if (cardButtonTracker[i] > cardButtonTracker[currentButton])
                        {
                            cardButtonTracker[i]--;
                            if (cardButtonTracker[i] == 0)
                            {
                                nextButton = i;
                            }
                        }
                    }
                    cardButtonTracker[currentButton] = -1;
                    if (nextButton != -1 )
                    {
                        if (!handRegion.CardPressed(nextButton))
                        {
                            cardButtonTracker[nextButton] = -1;
                        }
                    }
                } while (nextButton != -1 && cardButtonTracker[nextButton] == -1);
            }
        }
        #endregion
    }
}
